
pn = 0;
direction = true;
inital = true;
playing = false;
playingduration = 1500;
var scrollFunc=function(event){
    event=event || window.event;
    if (!playing) {
    	if (event.wheelDelta < 0) {
    		motion(true);
    	}else{
    		motion(false);
    	}
    };
}
if(document.addEventListener){
    document.addEventListener('DOMMouseScroll',scrollFunc,false);
}
window.onmousewheel=document.onmousewheel=scrollFunc;
document.onkeydown = function(event) {
		event=event || window.event;
		var c = event.keyCode;
		if (c==40||c==32||c==39) {
			motion(true);
		}else if (c==38||c==37) {
			motion(false);
		}
	}
function playingdelay(){
	playing = true;
	setTimeout(function(){playing = false;},playingduration);
}
function motion (direction) {
	if (direction == true) {
		if (pn == 3) {
			pn = 0;
			core(0,true);
			playingdelay();
		}else{
			pnsnap = pn;
			pn += 1;
			core(pn,false);
			playingdelay();
		}
	}else{
		if (pn == 0) {
		}else{
			pnsnap = pn;
			pn -= 1;
			core(pn,false);
			playingdelay();
		}
	}

}
function core (pn,loop){
	if(typeof(pgvMain) == 'function') {
		pgvSendClick({hottag:'browser.qb8.scroll'+(pn+1)});
	}
	// var bg = $('#container');
	$('#add_nav div').removeClass('on');
	$('#add_nav div:nth-child('+(pn+1)+')').addClass('on');
	var bg = $('#bg');
	var pub_t = 1000;
	var bgspeed = pub_t*4;
	if(pn == 0) {
		$('#add_links').css('display','none');
		$('#add_scrolling').velocity({opacity:1},{duration:1000});
		$('#add_lefttop').velocity({opacity:0},{duration:pub_t});
		bg.velocity({opacity:1,scale:0.6,translateX:'0px',translateY:'0px',rotateZ:'0deg'},{duration:pub_t*3,easing:'ease-in-out'});
	}else if (pn == 1) {
		$('#gotomac').css('display','none');
		$('#add_scrolling').velocity({opacity:0},{duration:500});
		$('#add_lefttop').velocity({opacity:1},{duration:pub_t});
		bg.velocity('stop').velocity({scale:1,translateX:'-200px',translateY:'300px',rotateZ:'-60deg'},{duration:pub_t*3,easing:'ease-in-out'});
	}else if (pn == 2) {
		$('#add_links').velocity({opacity:0},{duration:1500});
		// bg.velocity('stop').velocity({translateX:'-300px',translateY:'200px',rotateZ:'0deg'},{duration:pub_t*4,easing:'ease-in-out'});
	}else if (pn == 3) {
		$('#add_links').css('display','block').velocity({opacity:1},{duration:1500});
		// bg.velocity('stop').velocity({translateX:'-400px',translateY:'300px',rotateZ:'-30deg'},{duration:pub_t*4,easing:'ease-in-out'});
	};
	if (pn == 0) {
		// $('#p2_pop_1 img').velocity({translateX:'700px'},{duration:6000,loop:true});
		// $('#p2_pop_2 img').velocity({translateX:'600px'},{duration:6000,loop:true});
		// $('#p2_pop_3 img').velocity({translateX:'500px'},{duration:6000,loop:true});
		// $('#p2_pop_4 img').velocity({translateX:'-700px'},{duration:6000,loop:true});
		// loop
		if(loop == true){
			setTimeout(function(){
				p3.css('visibility','hidden');
			},1000)

				p2t1.velocity({opacity:1,translateZ:1100,rotateX:'-30deg'},{duration:pub_t});
				p2pop1.velocity({rotateY:'30deg',translateZ:'1300px',rotateX:'-180deg'},{duration:pub_t});
				p2pop2.velocity({rotateY:'30deg',translateZ:'1400px',rotateX:'-180deg'},{duration:pub_t});
				p2pop3.velocity({rotateY:'30deg',translateZ:'1500px',rotateX:'-180deg'},{duration:pub_t});
				p2pop4.velocity({rotateY:'-30deg',translateZ:'1600px',rotateX:'180deg'},{duration:pub_t});
		}
		// p0 init & back
		p081.velocity({rotateY:'20deg',translateZ:'1000px',rotateX:'180deg'},{duration:0});
		p082.velocity({rotateY:'20deg',translateZ:'1500px',rotateX:'360deg'},{duration:0});
		p0t1.velocity({opacity:0,translateZ:0,rotateY:0,rotateX:0},{duration:0});
		// p0t2.velocity({opacity:0,translateZ:0,rotateY:0,rotateX:0},{duration:0});
		slogan1.velocity({translateZ:'1000px',rotateY:'-60deg',translateY:'500px',opacity:1},{duration:0});
		slogan2.velocity({translateZ:'1300px',rotateY:'-60deg',translateY:'500px',opacity:1},{duration:0});
		slogan3.velocity({translateZ:'1600px',rotateY:'-60deg',translateY:'500px',opacity:1},{duration:0});
		// p0 in
		p081.velocity({rotateY:0,translateZ:'0px',rotateX:0},{duration:pub_t*2});
		p082.velocity({rotateY:0,translateZ:'0px',rotateX:0},{duration:pub_t*2.4});
		p0t1.velocity({opacity:1,rotateY:0,rotateX:0},{duration:pub_t/2,delay:pub_t*2});
		// p0t2.velocity('fadeIn',{duration:pub_t,delay:pub_t*2});
		slogan1.velocity({translateZ:'0px',rotateY:'0deg',translateY:'0px'},{duration:pub_t*2});
		slogan2.velocity({translateZ:'0px',rotateY:'0deg',translateY:'0px'},{duration:pub_t*2,delay:pub_t*0.6});
		slogan3.velocity({translateZ:'0px',rotateY:'0deg',translateY:'0px'},{duration:pub_t*2.2,delay:pub_t*0.6});

		setTimeout(function(){
			p0btn.css('display','block');
			p0btn.velocity({opacity:1},{duration:pub_t,delay:pub_t/2});
		},1400);

		// all page initalization

		// p1 back
			p1ui0.velocity({rotateY:'30deg',translateZ:'1000px',opacity:0},{duration:pub_t*1});
			p1t1.velocity({rotateX:'0deg',translateZ:'0px',rotateY:'20deg',opacity:0},{duration:pub_t*1});
			p1t2.velocity({opacity:0},{duration:pub_t*1});
	}
}
$(document).ready(function(){
    core(0,false);
    mac();
});
